Conditions | 1 |
Paths | 1 |
Total Lines | 115 |
Lines | 115 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* global tippy */ |
||
6 | var PronamicPayGatewayConfigEditor = function( element ) { |
||
7 | var obj = this; |
||
8 | var $element = $( element ); |
||
9 | |||
10 | // Elements |
||
11 | var elements = {}; |
||
12 | elements.variantId = $element.find( '#pronamic_gateway_id' ); |
||
13 | elements.extraSettings = $element.find( 'div.extra-settings' ); |
||
14 | elements.sectionHeaders = $element.find( '.gateway-config-section-header' ); |
||
15 | elements.tabs = $element.find( '.pronamic-pay-tabs' ); |
||
16 | elements.tabItems = $element.find( 'ul.pronamic-pay-tabs-items' ); |
||
17 | elements.pkCertFieldsToggle = $( '#pk-cert-fields-toggle' ); |
||
18 | |||
19 | /** |
||
20 | * Update config fields |
||
21 | */ |
||
22 | this.updateFields = function() { |
||
23 | // Find selected variant |
||
24 | obj.selectedVariant = elements.variantId.find( 'option:selected' ); |
||
25 | |||
26 | obj.settings = obj.selectedVariant.data( 'pronamic-pay-settings' ); |
||
27 | |||
28 | // Hide all settings |
||
29 | $element.find( '.extra-settings' ).hide(); |
||
30 | |||
31 | // Show settings for variant |
||
32 | obj.settingElements = []; |
||
33 | |||
34 | if ( $.isArray( obj.settings ) ) { |
||
35 | $.each( obj.settings, function( index, value ) { |
||
36 | $element.find( '.setting-' + value ).show(); |
||
37 | } ); |
||
38 | } |
||
39 | |||
40 | $element.find( '.setting-' + obj.selectedVariant.val() ).show(); |
||
41 | |||
42 | // Set name of first tab item to name of selected provider |
||
43 | var providerName = obj.selectedVariant.text().split( ' - ' )[0].replace( / \(.*\)/, '' ); |
||
44 | |||
45 | elements.tabItems.find( ':visible' ).first().text( providerName ).click(); |
||
46 | |||
47 | $( '#pronamic-pay-gateway-description').html( obj.selectedVariant.attr( 'data-gateway-description' ) ); |
||
48 | |||
49 | if ( elements.pkCertFieldsToggle.length > 0 ) { |
||
50 | elements.extraSettings.find( 'tr.pk-cert' ).hide(); |
||
51 | } |
||
52 | }; |
||
53 | |||
54 | // Update row background color |
||
55 | this.updateRowBackgroundColor = function() { |
||
56 | // Set background color of visible even rows |
||
57 | var rows = elements.extraSettings.find( '.form-table tr' ); |
||
58 | |||
59 | rows.removeClass( 'even' ); |
||
60 | rows.filter( ':visible:even' ).addClass( 'even' ); |
||
61 | }; |
||
62 | |||
63 | /** |
||
64 | * Tabs |
||
65 | */ |
||
66 | this.initTabs = function() { |
||
67 | $.each(elements.sectionHeaders, function ( i, elm ) { |
||
68 | var item = $( elm ); |
||
69 | var title = item.find( 'h4' ).text(); |
||
70 | var settingsClasses = item.parents( 'div' )[0].className; |
||
71 | |||
72 | elements.tabItems.append( |
||
73 | $( '<li>' + title + '</li>' ).addClass( settingsClasses ).removeClass( 'pronamic-pay-tab' ) |
||
74 | ); |
||
75 | } ); |
||
76 | |||
77 | // Move tab items list after 'Mode' setting |
||
78 | elements.tabItems.next().after( elements.tabItems ); |
||
79 | |||
80 | elements.tabItems.find( 'li' ).click( obj.showTabSettings ); |
||
81 | }; |
||
82 | |||
83 | this.showTabSettings = function() { |
||
84 | var tabItem = $( this ); |
||
85 | |||
86 | // Show tab |
||
87 | elements.extraSettings.hide().eq( tabItem.index() ).show(); |
||
88 | }; |
||
89 | |||
90 | this.togglePkCertFields = function( e ) { |
||
91 | if ( e.preventDefault ) { |
||
92 | e.preventDefault(); |
||
93 | } |
||
94 | |||
95 | if ( elements.pkCertFieldsToggle.hasClass( 'active' ) ) { |
||
96 | elements.pkCertFieldsToggle.removeClass( 'active' ); |
||
97 | |||
98 | elements.extraSettings.find( 'tr.pk-cert' ).hide(); |
||
99 | } else { |
||
100 | elements.pkCertFieldsToggle.addClass( 'active' ); |
||
101 | |||
102 | elements.extraSettings.find( 'tr.pk-cert' ).show(); |
||
103 | } |
||
104 | |||
105 | obj.updateRowBackgroundColor(); |
||
106 | |||
107 | return false; |
||
108 | }; |
||
109 | |||
110 | /** |
||
111 | * Function calls |
||
112 | */ |
||
113 | obj.initTabs(); |
||
114 | |||
115 | obj.updateFields(); |
||
116 | |||
117 | elements.variantId.change( obj.updateFields ); |
||
118 | |||
119 | elements.pkCertFieldsToggle.click( obj.togglePkCertFields ); |
||
120 | }; |
||
121 | |||
384 |